package com.orcwar.enums;

import java.util.HashMap;
import java.util.Map;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.orcwar.R;
import com.orcwar.engine.animation.Animation;
import com.orcwar.util.BitmapUtil;

public enum SpriteEnum {
	
	//ATTACK ANIMATIONS
	ATTACK_FLEAUTEUR(R.drawable.attack_fleauteur, 4, 2, new Animation("attack", new int[] { 0, 1, 2, 3, 4, 5, 6 }, 25)),
	ATTACK_TIREUR(R.drawable.attack_tireur, 12, 1, new Animation("attack", new int[] { 0, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11 }, 30)),

	// TILES
	TILE_BACKGROUND(R.drawable.tile), 
	TILE_WALL(R.drawable.tile_wall), 
	TILE_WATER(R.drawable.tile_water), 
	TILE_SELECTED_UNIT(R.drawable.tile_selected_unit), 
	TILE_WALK_PATH(R.drawable.tile_walk_path),
	TILE_ATTACK(R.drawable.tile_attack),
	TILE_MOVE(R.drawable.tile_move),
	TILE_ON_SHOW_PRESS_ATTACK(R.drawable.tile_on_show_pressed_attack),

	// UNITS
	UNIT_HACHEUR(R.drawable.unit_hacheur),
	UNIT_HOMMELANCE(R.drawable.unit_hommelance),
	UNIT_TIREURDELOIN(R.drawable.unit_tireur),
	UNIT_BOUCLIEUR(R.drawable.unit_bouclieur),
	UNIT_FLEAUTEUR(R.drawable.unit_fleauteur);

	private Bitmap bitmap;
	private int nbColumn;
	private int nbRows;
	private Map<String, Animation> animations;

	/**
	 * Constructor.
	 * @param _id the resource id
	 */
	SpriteEnum(int _id) {
		bitmap = BitmapUtil.createBitmap(_id);
	}
	
	
	/**
	 * Constructor.
	 * 
	 * @param _id
	 *            the resource id
	 * @param _nbColumn
	 *            the number of columns in the spritesheet
	 * @param _nbRows
	 *            the number of rows in the spritesheet
	 * @param _anims
	 *            the animations
	 */
	SpriteEnum(int _id, int _nbColumn, int _nbRows, Animation... _anims) {
		bitmap = BitmapUtil.createBitmap(_id);

		nbColumn = _nbColumn;
		nbRows = _nbRows;

		animations = new HashMap<String, Animation>();
		for (Animation anim : _anims) {
			animations.put(anim.getName(), anim);
		}
	}

	/**
	 * @return the bitmap
	 */
	public Bitmap getBitmap() {
		return bitmap;
	}

	/**
	 * @param bitmap
	 *            the bitmap to set
	 */
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public static Bitmap getTileBitmap(OWTileType tileType) {
		switch (tileType) {
		case GROUND:
			return TILE_BACKGROUND.getBitmap();

		case WALL:
			return TILE_WALL.getBitmap();

		case WATER:
			return TILE_WATER.getBitmap();

		default:
			return null;
		}
	}

	public void draw(Canvas canvas, RectF destRect, Paint paint,
			DirectionEnum orientation) {
		if (orientation != DirectionEnum.UP) {
			// Case of rotation
			float rotationAngle = 0;
			switch (orientation) {
			case DOWN:
				rotationAngle = 180;
				break;
			case LEFT:
				rotationAngle = -90;
				break;
			case RIGHT:
				rotationAngle = 90;
				break;
			default:
				break;
			}
			
			canvas.save();
			canvas.rotate(rotationAngle, destRect.left
					+ (bitmap.getWidth() / 2),
					destRect.top + (bitmap.getHeight() / 2));
			canvas.drawBitmap(bitmap, null, destRect, paint);
			canvas.restore();
		} else {
			// No rotation
			canvas.drawBitmap(bitmap, null, destRect, paint);
		}
	}

	/**
	 * @return the nbColumn
	 */
	public int getNbColumn() {
		return nbColumn;
	}

	/**
	 * @param nbColumn the nbColumn to set
	 */
	public void setNbColumn(int nbColumn) {
		this.nbColumn = nbColumn;
	}

	/**
	 * @return the nbRows
	 */
	public int getNbRows() {
		return nbRows;
	}

	/**
	 * @param nbRows the nbRows to set
	 */
	public void setNbRows(int nbRows) {
		this.nbRows = nbRows;
	}

	/**
	 * @return the animations
	 */
	public Map<String, Animation> getAnimations() {
		return animations;
	}

	/**
	 * @param animations the animations to set
	 */
	public void setAnimations(Map<String, Animation> animations) {
		this.animations = animations;
	}

}
